package com.xx.pmis.web.actions;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.TourismPlan;
import com.xx.pmis.model.TravellingExpense;
import com.xx.pmis.services.TourismPlanService;
import com.xx.pmis.services.TravellingExpenseService;
import com.xx.pmis.web.forms.TravellingExpenseForm;
import com.xx.pmis.web.util.AccountUtil;
import com.xx.pmis.web.util.CurrentDate;
import com.xx.pmis.web.util.Rand;

/**
 * @class 交通费用Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class TravellingExpenseAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(TravellingExpenseAction.class);

	TourismPlanService tourismPlanService = getPMISServices().getTourismPlanService();
	TravellingExpenseService travellingExpenseService = getPMISServices().getTravellingExpenseService();

	AccountUtil accountUtil = new AccountUtil();

	// 查询所有交通费用
	public ActionForward listTravellingExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<TravellingExpense> travellingExpenseList = null;
		try {
			travellingExpenseList = travellingExpenseService
					.getAllTravellingExpenseByCondition(" order by teDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费】查询【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("position", "旅游计划/交通费");
		request.setAttribute("travellingExpenseList", travellingExpenseList);
		return mapping.findForward("listTravellingExpense");
	}

	// 新增
	public ActionForward addTravellingExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService
					.getAllTourismPlanByCondition(" where tpState = '1' order by beginDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/新增】查询未完成旅游计划异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		if (tourismPlanList.size() == 0) {
			String errorPage = "【旅游计划/交通费/新增】没有未完成旅游计划，无法添加交通费用！";
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("teDate", CurrentDate.currentDate());
		request.setAttribute("tourismPlanList", tourismPlanList);
		request.setAttribute("position", "交通费/新增");
		return mapping.findForward("addTravellingExpense");
	}

	// 新增，处理方法
	public ActionForward addTravellingExpense_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		TravellingExpenseForm f = (TravellingExpenseForm) form;
		String teDate = f.getTeDate(); // 业务日期，业务日期在结算时作为流水账记账日期
		String teType = f.getTeType(); // 交通类型 火车 0，汽车 1，飞机 2，地铁 3，轮船 4
		String beginPlace = f.getBeginPlace(); // 出发点
		String endPlace = f.getEndPlace(); // 到达站
		String trainNum = f.getTrainNum(); // 车次，仅限于火车
		String teNumber = f.getTeNumber(); // 数量
		String teTime = f.getTeTime(); // 耗时
		String teExpenses = f.getTeExpenses(); // 费用
		String teDesc = f.getTeDesc(); // 描述
		String tpID = f.getTpID(); // 旅游计划ID

		// 交通费
		TravellingExpense travellingExpense = new TravellingExpense();
		travellingExpense.setTeID(CurrentDate.currentYear() + Rand.getRand());
		travellingExpense.setTeDate(teDate);
		travellingExpense.setTeType(teType);
		travellingExpense.setBeginPlace(beginPlace);
		travellingExpense.setEndPlace(endPlace);
		travellingExpense.setTrainNum(trainNum);
		travellingExpense.setTeNumber(teNumber);
		travellingExpense.setTeTime(teTime);
		travellingExpense.setTeExpenses(accountUtil.if1ZeroTo2Zero(teExpenses));
		travellingExpense.setTeDesc(teDesc);
		travellingExpense.setTePay("1");
		travellingExpense.setTpID(tpID);

		try {
			travellingExpenseService.saveOrUpdateTravellingExpense(travellingExpense);
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/新增】保存或更新【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		listTravellingExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listTravellingExpense");
	}

	// 编辑
	public ActionForward editTravellingExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String teID = request.getParameter("teID");
		String pageNum = request.getParameter("pageNum");

		TravellingExpense travellingExpense = null;
		try {
			travellingExpense = travellingExpenseService.getTravellingExpenseByTravellingExpenseID(teID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/编辑】根据【交通费用ID】查询该【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(travellingExpense.getTpID());
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/编辑】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		List<TourismPlan> tourismPlanList = null;
		try {
			tourismPlanList = tourismPlanService
					.getAllTourismPlanByCondition(" where tpState = '1' order by beginDate desc");
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/编辑】查询未完成【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		request.setAttribute("tpName", tourismPlan.getTpName());
		request.setAttribute("tourismPlanList", tourismPlanList);
		request.setAttribute("position", "交通费/编辑");
		request.setAttribute("travellingExpense", travellingExpense);
		return mapping.findForward("editTravellingExpense");
	}

	// 编辑，处理方法
	public ActionForward editTravellingExpense_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");

		TravellingExpenseForm f = (TravellingExpenseForm) form;
		String teID = f.getTeID(); // 费用ID
		String teDate = f.getTeDate(); // 业务日期，业务日期在结算时作为流水账记账日期
		String teType = f.getTeType(); // 交通类型 火车 0，汽车 1，飞机 2，地铁 3，轮船 4
		String beginPlace = f.getBeginPlace(); // 出发点
		String endPlace = f.getEndPlace(); // 到达站
		String trainNum = f.getTrainNum(); // 车次，仅限于火车
		String teNumber = f.getTeNumber(); // 数量
		String teTime = f.getTeTime(); // 耗时
		String teExpenses = f.getTeExpenses(); // 费用
		String teDesc = f.getTeDesc(); // 描述

		// 交通费
		TravellingExpense travellingExpense = null;
		try {
			travellingExpense = travellingExpenseService.getTravellingExpenseByTravellingExpenseID(teID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/编辑】根据【交通费用ID】查询该【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		travellingExpense.setTeDate(teDate);
		travellingExpense.setTeType(teType);
		travellingExpense.setBeginPlace(beginPlace);
		travellingExpense.setEndPlace(endPlace);
		travellingExpense.setTrainNum(trainNum);
		travellingExpense.setTeNumber(teNumber);
		travellingExpense.setTeTime(teTime);
		travellingExpense.setTeExpenses(accountUtil.if1ZeroTo2Zero(teExpenses));
		travellingExpense.setTeDesc(teDesc);
		// travellingExpense.setTePay("1");

		try {
			travellingExpenseService.saveOrUpdateTravellingExpense(travellingExpense);
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/编辑】保存或更新【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listTravellingExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listTravellingExpense");
	}

	// 删除
	public ActionForward delTravellingExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String pageNum = request.getParameter("pageNum");
		// 交通费
		TravellingExpense travellingExpense = null;
		TravellingExpenseForm f = (TravellingExpenseForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				travellingExpense = travellingExpenseService.getTravellingExpenseByTravellingExpenseID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【旅游计划/交通费/删除】根据【交通费用ID】查询该【交通费】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				travellingExpenseService.deleteTravellingExpense(travellingExpense);
			} catch (Exception e) {
				String errorPage = "【旅游计划/交通费/删除】保存更新【交通费】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		request.setAttribute("pageNum", pageNum);
		listTravellingExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listTravellingExpense");
	}

	// 结算
	public ActionForward payTravellingExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String teID = request.getParameter("teID");
		String pageNum = request.getParameter("pageNum");

		TravellingExpense travellingExpense = null;
		try {
			travellingExpense = travellingExpenseService.getTravellingExpenseByTravellingExpenseID(teID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/删除】根据【旅游计划ID】查询该【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		TourismPlan tourismPlan = null;
		try {
			tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(travellingExpense.getTpID());
		} catch (Exception e) {
			String errorPage = "【旅游计划/住宿费/结算】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		// 生成记账凭证
		// 科目代码
		String subjectCode = "";
		if (tourismPlan.getTpType().equals("0")) {
			// 旅游 0
			subjectCode = "0030005";
		} else if (tourismPlan.getTpType().equals("1")) {
			// 出差 1
			subjectCode = "0030012";
		} else {
			// 探亲 2
			subjectCode = "0030018";
		}
		accountUtil.payExpenseToAccount(travellingExpense.getTeDate(), subjectCode, travellingExpense.getTeNumber(),
				travellingExpense.getTeExpenses(), tourismPlan.getTpName(), "/" + travellingExpense.getTeType());
		// 更新结算状态，结算完成为"0"
		travellingExpense.setTePay("0");
		try {
			travellingExpenseService.saveOrUpdateTravellingExpense(travellingExpense);
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/结算】保存或更新【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		request.setAttribute("pageNum", pageNum);
		listTravellingExpenseByPage(mapping, form, request, response);
		return mapping.findForward("listTravellingExpense");
	}

	// 分页显示
	public ActionForward listTravellingExpenseByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = travellingExpenseService.getCountOfTravellingExpenseByCondition("");
		} catch (Exception e) {
			String errorPage = "【旅游管理/交通费】查询【交通费】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}
		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<TravellingExpense> travellingExpenseList = null;
		List<TravellingExpense> travellingExpenseListTemp = new ArrayList<TravellingExpense>();
		try {
			travellingExpenseList = travellingExpenseService.getAllTravellingExpenseByConditionByPage(
					" order by teDate desc", Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【旅游管理/交通费】查询【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		for (TravellingExpense travellingExpense : travellingExpenseList) {
			TourismPlan tourismPlan = null;
			try {
				tourismPlan = tourismPlanService.getTourismPlanByTourismPlanID(travellingExpense.getTpID());
			} catch (Exception e) {
				String errorPage = "【旅游计划/交通费用/分页显示】根据旅游计划ID查询该【旅游计划】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			travellingExpense.setTpName(tourismPlan.getTpName());
			travellingExpenseListTemp.add(travellingExpense);

		}
		request.setAttribute("travellingExpenseList", travellingExpenseListTemp);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}
		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "travellingExpense.do");
		request.setAttribute("position", "旅游管理/交通费");
		return mapping.findForward("listTravellingExpense");
	}

	// 明细
	public ActionForward detailTravellingExpense(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String teID = request.getParameter("teID");
		TravellingExpense travellingExpense = null;
		try {
			travellingExpense = travellingExpenseService.getTravellingExpenseByTravellingExpenseID(teID);
		} catch (Exception e) {
			String errorPage = "【旅游计划/交通费/明细】根据【交通费用ID】查询该【交通费】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("position", "交通费/明细");
		request.setAttribute("travellingExpense", travellingExpense);
		return mapping.findForward("detailTravellingExpense");
	}

}